//
//  MainPageSearchViewController.m
//  YingKeEducation
//
//  Created by 戴伟 on 2019/1/30.
//  Copyright © 2019 戴伟. All rights reserved.
//

#import "SearchViewController.h"

#import "TTGTextTagCollectionView.h"


@interface SearchViewController ()<UITextFieldDelegate, TTGTextTagCollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *recordArr;
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITextField *textField;



@property (strong, nonatomic) IBOutlet UIView *historyView;
@property (nonatomic, strong) TTGTextTagCollectionView *tagCollectionView;
@property (nonatomic, copy) NSString *keyword;

@end

@implementation SearchViewController



- (BOOL)fd_prefersNavigationBarHidden
{
    return YES;
}

-(instancetype)initWithKeyword:(NSString *)keyword{
    if (self = [super init]) {
        _keyword = keyword;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"搜索";
    [self setupUI];
}

-(void)setupUI{
    
    [self.navView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(SafeAreaTopHeight);
    }];
    
    [self.view addSubview:self.historyView];
    [self.historyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_offset(0);
        make.top.equalTo(self.navView.mas_bottom);
        make.right.mas_offset(0);
    }];
    
    _tagCollectionView = [TTGTextTagCollectionView new];
    [_historyView addSubview:_tagCollectionView];
    [_tagCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_offset(12);
        make.right.mas_offset(-12);
        make.top.mas_offset(68);
    }];
//    _tagCollectionView.enableTagSelection = NO;
    _tagCollectionView.backgroundColor = [UIColor whiteColor];
    _tagCollectionView.delegate = self;

    //搜索历史
    _recordArr = [NSMutableArray array];
    NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"searchHistory"];
    NSArray *arr = [str mj_JSONObject];
    [_recordArr addObjectsFromArray:arr];
    
    for (NSString *str in _recordArr) {
        TTGTextTagConfig *config = [[TTGTextTagConfig alloc] init];
        config.borderWidth = 0;
        config.cornerRadius = 10;
        config.textColor = BLACKTEXTCOLOR;
        config.shadowOpacity = 0;
        config.extraSpace = CGSizeMake(24, 14);
        config.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7" andAlpha:1];
        config.textFont = [UIFont systemFontOfSize:13];
        config.selectedTextColor = BLACKTEXTCOLOR;
        config.selectedCornerRadius = 10;
        config.selectedBackgroundColor = [UIColor colorWithHexString:@"#F7F7F7" andAlpha:1];
        [_tagCollectionView addTag:str withConfig:config];
    }
    
    _textField.returnKeyType = UIReturnKeySearch;
    
//    [self.textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.textField becomeFirstResponder];
    if (_keyword && _keyword.length > 0) {
        _textField.text = _keyword;
    }
}





- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.textField endEditing:YES];
    if (textField.text.length > 0) {
        if (![self.recordArr containsObject:textField.text]) {
            [self.recordArr insertObject:textField.text atIndex:0];
            [[NSUserDefaults standardUserDefaults] setObject:[self.recordArr mj_JSONString] forKey:@"searchHistory"];
            
            TTGTextTagConfig *config = [[TTGTextTagConfig alloc] init];
            config.borderWidth = 0;
            config.cornerRadius = 10;
            config.textColor = BLACKTEXTCOLOR;
            config.selectedTextColor = BLACKTEXTCOLOR;
            config.selectedCornerRadius = 10;
            config.selectedBackgroundColor = [UIColor colorWithHexString:@"#F7F7F7" andAlpha:1];
            config.shadowOpacity = 0;
            config.extraSpace = CGSizeMake(20, 10);
            config.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7" andAlpha:1];
            config.textFont = [UIFont systemFontOfSize:13];
            [_tagCollectionView insertTag:textField.text atIndex:0 withConfig:config];
        }
        if (self.searchWithKeyword) {
            self.searchWithKeyword(textField.text);
        }
        [self.navigationController popViewControllerAnimated:YES];
        return YES;
    }
    return NO;
    
}

//-(void)textFieldChange:(UITextField *)textField{
//    if (textField.text.length == 0) {
//        self.historyView.hidden = NO;
//
//    }
//}



-(void)textTagCollectionView:(TTGTextTagCollectionView *)textTagCollectionView didTapTag:(NSString *)tagText atIndex:(NSUInteger)index selected:(BOOL)selected tagConfig:(TTGTextTagConfig *)config{
    NSString *str = _recordArr[index];
    _textField.text = str;
    if (self.searchWithKeyword) {
        self.searchWithKeyword(str);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)clearHistory:(id)sender {
    [_tagCollectionView removeAllTags];
    [self.recordArr removeAllObjects];
    [[NSUserDefaults standardUserDefaults] setObject:[self.recordArr mj_JSONString] forKey:@"searchHistory"];
}

@end
